/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.csm.csmstaffhelper;

import com.sun.java.swing.plaf.windows.resources.windows_de;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.csm.csmstaffhelper.gui.MainWindow;

/**
 *
 * @author exception
 */
public class StaffSearcher {
    private CsmProperties props;

    public StaffSearcher(CsmProperties props) {
        this.props = props;
    }

    public boolean searchStaff(final MainWindow window) throws IOException, URISyntaxException {
        boolean found = false;

        String pageContent = getHtmlContent(props.getSid(), props.getUrl());

        List levelsList = getStuffLevelsList(pageContent);
        Iterator it = levelsList.iterator();
        while (it.hasNext()) {
            StaffLevel level = (StaffLevel) it.next();

            window.moveProgressBar();

            if (level.compareTo(props.getBaseLevel(), props.getStricted()) == 1) {
//                            System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":" + Calendar.getInstance().get(Calendar.MINUTE) + " Found staff with levels: " + level);
                //TODO make listeners clearCache().
                window.printText(Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + ":" + Calendar.getInstance().get(Calendar.MINUTE) + " Found staff with levels: " + level);

                found = true;
                
                Alarm.openUrl(props.getUrl());

                Alarm.playSound(props.getAlarm());
            }
        }

        return found;
    }

    private String getHtmlContent(String sid, String url) throws IOException, MalformedURLException {
        URL csmUrl = new URL(url);
        URLConnection csmConnection = csmUrl.openConnection();

        csmConnection.setRequestProperty("Cookie", "sid=" + sid);

        csmConnection.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(csmConnection.getInputStream()));
        String inputLine;
        StringBuilder builder = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            builder.append(inputLine + "\n");
        }
        in.close();

        return builder.toString();
    }

    private List getStuffLevelsList(String pageContent) {
        String value = "class=\"value\"><img src=\"/images/activity_";
        int index = 0;
        int count = 0;
        Integer[] levels = new Integer[3];
        ArrayList newLevels = new ArrayList();

        while (index != -1) {
            count++;
            if (index == 0) {
                index = pageContent.indexOf(value);
            } else {
                index = pageContent.indexOf(value, index + value.length());
            }

            if (index == -1) {
                break;
            }

            levels[count - 1] = Integer.parseInt(Character.toString(pageContent.charAt(index + value.length())));

            if (count % 3 == 0 && count > 0) {
                newLevels.add(new StaffLevel(levels));

                count = 0;
                levels = new Integer[3];
            }
        }

        return newLevels;
    }
}
